import requests
import json
import os
import pandas as pd
import realtime_vote


# 调用realtime_vote模块，获取投票数据
res3= realtime_vote.get_vote()

now_time= res3["jData"]["nowtime"]
mvote :dict = res3["jData"]["membervotes"]

# 导入Excel表格，各列均转换成列表
mlist = pd.read_excel("data/成员清单.xlsx")
index = mlist["index"].tolist()
mid = mlist["mid"].tolist()
tname = mlist["tname"].tolist()
nick = mlist["nick"].tolist()
name = mlist["name"].tolist()
pic = mlist["pic"].tolist()
road = mlist["road"].tolist()
roadclass = mlist["roadclass"].tolist()
vote = mlist["vote"].tolist()

# 更新vote列表
for i in mvote:     
    j= mid.index(int(i))
    vote[j]= mvote[i]
 

# vote字符串转数字
for i,j in enumerate(vote):
    vote[i] = int(vote[i])



# 构造matrix表格
def matrix (index, mid, tname, nick, name, pic, road, roadclass, vote):   
    matrix4 = {
        "index":index,
        "mid":mid,
        "tname":tname,
        "nick":nick,
        "name":name,
        "pic":pic,
        "road":road,
        "roadclass":roadclass,
        "vote":vote
        }
    df4 = pd.DataFrame(matrix4)
    return df4

# 生成表格和根据vote排序
df5= matrix(index, mid, tname, nick, name, pic, road, roadclass, vote)
(row5, column5) = df5.shape
df51= df5.nlargest(row5,"vote")
df52= df51.reset_index()

file_name = "总成员清单"+ str(now_time) + ".xlsx"
file_path2 = './data/'
full_path = os.path.join(file_path2, str(file_name))
df52.to_excel(full_path, sheet_name="members", index=True)



# 定义分路list，对抗路，打野，中路，发育路，游走
# 提取分路
# 列表推导式提取出"dkl"
def f_roadclass(which_roadclass):
    index_dkl= [i for i, j in enumerate(roadclass)   if j ==which_roadclass]
    m_index = []
    m_mid = []
    m_nick = []
    m_tname = []
    m_name = []
    m_pic = []
    m_road = []
    m_roadclass = []
    m_vote = []
    for k in index_dkl:
        m_index.append(index[k])
        m_mid.append(mid[k])
        m_nick.append(nick[k])
        m_tname.append(tname[k])
        m_name.append(name[k])
        m_pic.append(pic[k])
        m_road.append(road[k])
        m_roadclass.append(roadclass[k])
        m_vote.append(vote[k])


    df6= matrix(m_index, m_mid, m_tname, m_nick, m_name, m_pic, m_road, m_roadclass, m_vote)
    (row, column) = df6.shape

    df7= df6.nlargest(row,"vote")
    df8= df7.reset_index()
    file_name6 = which_roadclass +"成员清单"+ str(now_time) + ".xlsx"
    file_path6 = './data/'
    full_path6 = os.path.join(file_path6, str(file_name6))
    df8.to_excel(full_path6, sheet_name="members", index=True)
    return df8

#各分路表格
dkl= f_roadclass("dkl")
dy= f_roadclass("dy")
zl= f_roadclass("zl")
fyl= f_roadclass("fyl")
yz= f_roadclass("yz")

# print(dkl, dy, zl, fyl, yz)


print(dkl)
print(dy)
print(zl)
print(fyl)
print(yz)

# 导出为HTML字符串
html_dkl = dkl.to_html()
html_dy = dy.to_html()
html_zl = zl.to_html()
html_fyl = fyl.to_html()
html_yz = yz.to_html()

html_all = df52.to_html()

 
 
# 如果想要将HTML保存到文件中
with open('./to_html/dkl.html', 'w') as f:
    f.write(html_dkl)
with open('./to_html/dy.html', 'w') as f:
    f.write(html_dy)
with open('./to_html/zl.html', 'w') as f:
    f.write(html_zl)
with open('./to_html/fyl.html', 'w') as f:
    f.write(html_fyl)
with open('./to_html/yz.html', 'w') as f:
    f.write(html_yz)
with open('./to_html/all.html', 'w') as f:
    f.write(html_all)







